package com.leo.algorithm.排序;

/**
 * 插入排序
 */
public class InsertionSort {

    public static void main(String[] args) {
        int arr[]={2,4,1,6,5,8,9,10,3,7};
        int[] bub = insertionSort(arr);
        for (int i = 0; i <bub.length ; i++) {
            System.out.println(bub[i]);
        }
    }

    /**
     * 插入排序
     * @param array
     * @return
     */
    public static int[] insertionSort(int[] array) {
        if (array.length == 0)
            return array;
        int current;
        for (int i = 0; i < array.length - 1; i++) {
            current = array[i + 1];
            int preIndex = i;
            while (preIndex >= 0 && current < array[preIndex]) {
                array[preIndex + 1] = array[preIndex];
                preIndex--;
            }
            array[preIndex + 1] = current;
        }
        return array;
    }
}
